<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <#include "/common/plateform/include/common-base.ftl">
    <title>规则参数设置</title>
    <!-- Tell the browser to be responsive to screen width -->
    <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
</head>

<!-- Part 一、 以下是body主体内容的模块区域 -->
<body class="hold-transition ${sysColor} sidebar-mini">
<div class="wrapper">

    <!-- top start -->
    <#include "/common/plateform/top.ftl">
    <!-- top end -->
    <!-- left start -->
    <#include "/common/plateform/left-project.ftl">
    <!-- left end-->

    <div class="content-wrapper">
        <!-- Main start -->
        <section class="content">
            <div class="row">
                <div class="col-xs-12">

                    <div class="box box-success">
                        <div class="box-header with-border">
                            <h3 class="box-title"><i class="fa fa-cog fx2"></i> 规则参数设置
                                [${cateName!''}-${cateNo!''}]
                                <button  onclick="toBack('${page_admin}/project/sysRulesProd-sets-list');" class="btn btn-primary btn-xs" type="button"><i class="fa fa-reply"></i> 返回上级</button>
                            </h3>
                            <div class="box-tools pull-right">
                                <button type="button" class="btn btn-box-tool" data-widget="collapse"><i class="fa fa-minus"></i>
                                </button>
                            </div>
                            <form id="searchForm" action="javascript:;">
                                <!-- temp="searchForm_hidden" start -->
                                <div id="searchForm_hidden" style="display:;">
                                    <input type="hidden" name="pageName" value="sysRulesProd-list"/>
                                    <input type="hidden" name="formName" value="searchForm"/>
                                    <input type="hidden" name="sysRulesProd.w_sysName" value=""/>
                                    <input type="hidden" name="sysRulesProd.w_showFlag" value="T"/>
                                    <input type="hidden" name="sysRulesProd.w_cateId" value="${cateId!''}"/>
                                    <input type="hidden" name="sysRulesProd.w_cateNo" value="${cateNo!''}"/>
                                    <input type="hidden" name="orderBy" value="sysRulesProd.rules_no,sysRulesProd.order_no+0"/>
                                </div>
                            </form>
                        </div>
                        <div class="box-body" id="rulespro-body">


                        </div>
                        <div class="form-group box-footer clearfix hidden">
                            <div class="col-sm-5 pull-left">
                                <button type="button" class="btn btn-default" onclick="resetTransRules();"><i class="fa fa-cog"></i> 重新初始化参数</button> 
                            </div>
                        </div>
                    </div>
                    <!------- 列表 end -------->
                </div>
            </div>


            <!-- Part 二、以下是template模板定义的模块区域 -->

            <script type="text/template" id="rulespro-template">

                <? for ( var i = 0; i < rows.length; i++) {?>
                <div class="form-group col-lg-6" style="border: 1px #ccc dashed;">
                    <div>
                        <span class="form-control-static"><?=(i+1)?>.<?=rows[i].sysRulesProd.formulaName?></span>
                    </div>
                    <div>
                        <div class="input-group input-group-sm">
                            <input id="rsp-<?=rows[i].sysRulesProd.rulesParamId?>"
                               data-bv-regexp="<?=strUtil.doNull(rows[i].sysRulesProd.showValid,'')?>"
                               data-bv-regexp-message="<?=strUtil.doNull(rows[i].sysRulesProd.showTip,'必填项')?>"
                               onblur="saveRulesPro(this)" formulaSrc="<?=rows[i].sysRulesProd.formulaSrc?>"
                               paramNo="<?=rows[i].sysRulesProd.paramNo?>"
                               paramName="<?=rows[i].sysRulesProd.paramName?>"
                               rulesproid="<?=rows[i].sysRulesProd.rulesParamId?>"
                               class="form-control fx13 trans-rules"
                               value="<?=strUtil.doNull(rows[i].sysRulesProd.paramNo,'')?>" maxlength="12"/>
                            <span class="input-group-addon"><?=strUtil.doNull(rows[i].sysRulesProd.paramUnit,'')?></span>
                        </div>
                    </div>
                    <div class="f12 fx3">
                        <?if(strUtil.isNull(rows[i].sysRulesProd.showTip)){?>
                        *此项为必填项
                        <?}else{?>
                        <?=rows[i].sysRulesProd.showTip?>
                        <?}?>
                    </div>
                    <!--<span  class="col-sm-1 fx2"><i class="fa fa-remove"></i></span>-->
                </div>
                <?}?>
            </script>

            <!-- Part 三、 以下是js模块区域 -->
            <script>
                var clickMenu = "sysRulesProd-sets-list";
                var myPage,dataListId,searchFormId;
                myPage = 10;//定义列表数据
                dataListId = "dataList";//显示列表数据div的id
                searchFormId = "searchForm";//搜索表单Id
                var defSysEname = strUtil.doNull(strUtil.getLocalStorage("sysEname"), "");
                $("#span-sysname-text").text(defSysEname+"-"+strUtil.getLocalStorage("sysCname"));
                $("#"+searchFormId).find("input[name='sysRulesProd.w_sysName']").val(defSysEname);
                /**
                 * 初始化数据字典，id传入#id 返回值Map用法：dictListMap["userType"][rows[i].userType]
                 */
                var dictListMap = DictUtil.initDictAndDymic();

                $(function () {
                    //1、加载分页列表数据
                    getRulesAllList();

                    //2、控制是否启用token
                    //setToken();

                });
                //注入日历框样式
                var dateObject = $('.datepicker').datepicker( {
                    language : 'zh-CN',
                    //inline: true,
                    todayBtn : "linked",
                    format : "yyyy-mm-dd",
                    autoclose: true
                }).on('hide',function(e) {
                    //bootstrap datepicker与bootstrapValidator同时使用时选择日期后无法正常触发校验的解决方法
                    //当日期选择框关闭时，执行刷新校验。
                    var fieldName = $(this).attr("name");//获取当前元素name
                    var formId = $(this).parents("form").eq(0).attr("id");//获取当前元素所在父层级中的就近的formId
                    $("#"+formId).data('bootstrapValidator').updateStatus(fieldName,'NOT_VALIDATED',null).validateField(fieldName);
                });

                //给模态窗口内form表单内的所有支持必填项的元素注入校验
                $(".modal").on('shown.bs.modal', function () {
                    $(this).find("form").each(function(){
                        var formId = $(this).attr("id");
                        if(!strUtil.isNull(formId)){
                            formId="#"+formId;
                            setValidate(formId);//定义add表单验证属性
                            mytools.cleanValidate(formId);//清空上次校验规则提示
                        }
                    });
                });

                //获取规则
                function getRulesAllList(){
                    var a = layer.msg("数据加载中...");
                    $.ajax({
                        type:'get',
                        url:common_list_url,
                        data:$("#"+searchFormId).serialize(),
                        dataType:'json',
                        success: function(myData){
                            console.log("规则查询结果：",myData);
                            if(myData.flag="T"){
                                var data = baidu.template("rulespro-template",eval(myData));
                                $("#rulespro-body").html(data);
                                layer.close(a);
                            }
                        }
                    });

                }
                function toBack(url){
                    var page="1";
                    if(page){
                        sessionStorage.setItem("page",page);
                    }
                    mytools.pjax(url);
                }

                //重置交易规则
                function resetTransRules(){
                    var index_submit = layer.confirm('此操作将会删除所有已设规则参数值，进行重新初始化规则，您确认继续执行吗 ？', {btn: ['继续','放弃'],closeBtn: 0,offset: '220px'}
                        , function(){
                            setToken();
                            layer.close(index_submit);
                            var msgIndex = layer.msg("正在删除历史规则参数");
                            var paramLoadData = {
                                "pageName": "sysRulesProd-list",
                                "formName": "delForm",
                                "sysRulesProd.w_sysName": defSysEname
                            };
                            $.ajax({
                                type:'post',
                                url:common_submit_url,
                                data:paramLoadData,
                                dataType:'json',
                                success: function(myData){
                                    console.log('删除历史规则',myData);
                                    if(myData.flag="T"){
                                        msgIndex = layer.msg("正在初始化规则参数");
                                        // addTransRules();
                                        getRulesAllList();

                                    }
                                }
                            });
                        });
                }


                //自动保存每个规则参数值
                function saveRulesPro(o){
                    var obj = $(o);
                    var objId = obj.attr("id");
                    var paramNoNew = obj.val();

                    var pattern = obj.attr("data-bv-regexp");
                    var patternMess = obj.attr("data-bv-regexp-message");
                    /** if(strUtil.isNull(paramNoNew)){
                          layer.tips("不能为空", '#'+objId, {tips: 3});
                          $("#"+objId).focus();
                          return;
                      }
                     **/
                    var regObj = new RegExp(pattern);
                    var result = regObj.test(paramNoNew);
                    //var result = paramNoNew.match(regObj);
                    if(!result){
                        layer.tips(patternMess, '#'+objId, {tips: 3});
                        $("#"+objId).focus();
                        obj.val("");
                        return;
                    }

                    var paramName = obj.attr("paramName");
                    var paramNoOld = obj.attr("paramNo");
                    var rpid = obj.attr("rulesproid");

                    if(paramNoNew && paramNoNew!==paramNoOld){

                        var formulaSrc = obj.attr("formulaSrc");
                        var inputName = "{{param}}";
                        var formulaCalNew = formulaSrc;
                        if(formulaSrc.indexOf(inputName)>-1){
                            //formulaCalNew=formulaSrc.replace(inputName,paramNoNew);
                            formulaCalNew = formulaSrc.replace(new RegExp(inputName,"gm"),paramNoNew);//进行多行全局匹配
                        }
                        var paramData = {"pageName":"sysRulesProd-list","formName":"editForm","sysRulesProd.w_rulesParamId":rpid,"sysRulesProd.paramName":paramName,"sysRulesProd.paramNo":paramNoNew,"sysRulesProd.formulaCal":formulaCalNew};
                        $.ajax({
                            type:'post',
                            url:common_submit_url,
                            data:paramData,
                            dataType:'json',
                            success: function(myData){
                                console.log('edit.sysRulesProd.formulaCal.new',myData);
                                if(myData.flag="T"){
                                    obj.attr("paramNo",paramNoNew);
                                    layer.tips("保存成功", '#'+objId, {tips: 3});
                                }
                            }
                        });
                    }
                }

            </script>
        </section>
        <!-- Main end -->
    </div>
    <!-- foot start -->
    <#include "/common/plateform/foot.ftl">
    <!-- foot start -->
</div>
<!-- /.content-wrapper -->
</body>
</html>












